package com.demo.dao;

import com.demo.common.model.Flighttickets;
import com.demo.common.model.Flighttime;
import com.jfinal.kit.Kv;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.SqlPara;

import java.util.List;

public class FlightticketsDao {

    private Flighttickets dao = new Flighttickets().dao();

    public Page<Flighttickets> paginate(int pageNumber, int pageSize) {
        return dao.paginate(pageNumber, pageSize, "select *", "from flighttickets order by id asc");
    }

    public Page<Flighttickets> paginate(int pageNumber, int pageSize, Kv cond) {
        SqlPara sqlPara = dao.getSqlPara("searchFlighttickets", cond);
        return dao.paginate(pageNumber, pageSize, sqlPara);
    }

    public Flighttickets findById(int id) {
        return dao.findById(id);
    }

    public List<Flighttickets> findAll(){
        return dao.findAll();
    }

    public void deleteById(int id) {
        dao.deleteById(id);
    }

    public Flighttickets findByFlightticketsno(String id,String flightticketsno){
        String sql = "select * from flighttickets where flightticketsno='" + flightticketsno + "'";
        if (StrKit.notBlank(id)) {
            sql += " and id <>" + id;
        }
        return dao.findFirst(sql);
    }

    public Flighttickets findTicketNums(Integer id){
        String sql="select a.id,a.num,b.ticketnums from flighttickets a left join (select IFNULL(sum(ticketnum),0) ticketnums,flightticketsid from buyrecord GROUP BY flightticketsid) b on b.flightticketsid=a.id where a.id="+id;
        return dao.findFirst(sql);
    }
}
